<template>
  <el-drawer
    v-model="drawerVisible"
    :size="650"
    :title="titleMap[mode]"
    @closed="$emit('closed')"
    append-to-body
  >
    <el-container v-loading="saveLoading">
      <el-main>
        <el-form
          :model="form"
          :rules="rules"
          ref="saveForm"
          label-width="100px"
        >
          <el-form-item label="姓名" prop="name">
            <el-input
              v-model="form.name"
              placeholder="请输入姓名"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="属地" prop="block">
            <hh-dict
              v-model="form.block"
              dictType="block"
              placeholder="请选择属地"
              clearable
              filterable
            ></hh-dict>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input
              v-model="form.email"
              placeholder="请输入邮箱"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号码" prop="mobile">
            <el-input
              v-model="form.mobile"
              placeholder="请输入手机号码"
              clearable
            ></el-input>
            <div class="el-form-item-msg" v-if="mode == 'add'">
              创建账号后，手机号码为初始账号
            </div>
          </el-form-item>
          <el-form-item label="电话号码" prop="phone">
            <el-input
              v-model="form.phone"
              placeholder="请输入电话号码"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="身份证号码" prop="idCard">
            <el-input
              v-model="form.idCard"
              placeholder="请输入身份证号码"
              clearable
            ></el-input>
            <div class="el-form-item-msg" v-if="mode == 'add'">
              创建账号后，身份证号码后六位为初始密码
            </div>
          </el-form-item>

         
        </el-form>
      </el-main>
      <el-footer>
        <div class="form-footer">
          <el-button type="" @click="close">取消</el-button>
          <el-button type="primary" @click="submit">提交</el-button>
        </div>
      </el-footer>
    </el-container>
  </el-drawer>
</template>

<script>
export default {
  name: "user-save",
  emits: ["success", "closed"],
  data() {
    return {
      saveLoading: false,
      drawerVisible: false,
      mode: "add",
      titleMap: {
        add: "新增用户数据",
        edit: "编辑用户数据",
      },
      tip: "添加成功",
      form: {
        id: "",
        name: "",
        block: "",
        email: "",
        phone: "",
        mobile: "",
        idCard: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入姓名" },
          { min: 0, max: 200, message: "长度必须介于 0 和 200 之间" },
        ],
        block: [
          { required: true, message: "请选择企业属地" },
          {
            validator: async (rule, value, callback) => {
              let result = await this.checkDict(value, "block");
              if (result == 0) callback(new Error("请选择企业属地"));
              else callback();
            },
            trigger: "blur",
          },
        ],
        email: [
          { min: 0, max: 100, message: "长度必须介于 0 和 100 之间" },
          {
            pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
            message: "请输入正确的邮箱地址",
            trigger: "blur",
          },
        ],
        phone: [
          { min: 0, max: 20, message: "长度必须介于 0 和 20 之间" },
          {
            pattern: /\d{3}-\d{8}|\d{4}-\d{7}/,
            message: "请输入正确的电话号码",
            trigger: "blur",
          },
        ],
        mobile: [
          { required: true, message: "请输入手机号码" },
          { min: 0, max: 20, message: "长度必须介于 0 和 20 之间" },
          {
            pattern: /^1[34578]\d{9}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
        idCard: [
          { required: true, message: "请输入身份证号" },
          { min: 0, max: 20, message: "长度必须介于 0 和 18 之间" },
          {
            pattern:
              /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
            message: "请输入正确的身份证号",
            trigger: "blur",
          },
        ],
      },
      ifAdd: true,
    };
  },
  watch: {},
  mounted() {},
  methods: {
    //打开
    open(mode = "add") {
      this.mode = mode;
      this.drawerVisible = true;
      return this;
    },
    //修改
    async edit(id) {
      this.ifAdd = false;
      this.tip = "修改成功";
      this.drawerVisible = true;
      this.saveLoading = true;
      let params = { id: id };
      let res = await this.$API.user.details.get(params);
      if (res.code == 0) {
        this.form = res.data;
        this.saveLoading = false;
      } else {
        let errdata = this.$TOOL.response(res);
        this.$alert(
          this.$TOOL.json.get("", errdata, "message"),
          this.$TOOL.json.get("", errdata, "title"),
          {
            type: "error",
            confirmButtonText: "确定",
            dangerouslyUseHTMLString: true,
            showCancelButton: false,
          }
        )
          .then(() => {
            this.drawerVisible = false;
          })
          .catch(() => {});
      }
    },
    //提交
    submit() {
      this.$refs.saveForm.validate(async (valid) => {
        if (valid) {
          let res = await this.$API.user.save.post(this.form);
          if (res.code == 0) {
            this.$message.success(this.tip);
            //调用父类刷新
            this.$emit("onRefresh");
            this.drawerVisible = false;
          } else {
            let errdata = this.$TOOL.response(res);
            this.$alert(
              this.$TOOL.json.get("", errdata, "message"),
              this.$TOOL.json.get("", errdata, "title"),
              {
                type: "error",
                confirmButtonText: "确定",
                dangerouslyUseHTMLString: true,
                showCancelButton: false,
              }
            );
          }
        }
      });
    },
    //表单注入数据
    setData(data) {
      Object.assign(this.form, data);
    },
    // 关闭页面
    close() {
      this.$emit("closed");
    },
    //检测字典
    async checkDict(dictName, dictType) {
      let params = {
        dictName: dictName,
        dictType: dictType,
      };
      let res = await this.$API.dict.list.get(params);
      return res.data.length;
    },
  },
};
</script>

<style></style>
